import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class PhoneTaskQueueManager {
    private final ConcurrentHashMap<String, ConcurrentHashMap<Integer, PhoneTask>> taskMap = new ConcurrentHashMap<>();
    private final ConcurrentHashMap<String, PriorityBlockingQueue<PhoneTask>> queueMap = new ConcurrentHashMap<>();
    private final ExecutorService executorService = Executors.newCachedThreadPool();
    private final AtomicInteger taskCounter = new AtomicInteger();

    // 创建任务
    public synchronized void addTask(String phoneNumber, Runnable task, int priority) {
        int taskId = taskCounter.incrementAndGet();
        PhoneTask phoneTask = new PhoneTask(task, priority, taskId);

        queueMap.computeIfAbsent(phoneNumber, k -> new PriorityBlockingQueue<>()).add(phoneTask);
        taskMap.computeIfAbsent(phoneNumber, k -> new ConcurrentHashMap<>()).put(taskId, phoneTask);
    }

    // 插队任务
    public synchronized void addTaskWithHigherPriority(String phoneNumber, Runnable task) {
        addTask(phoneNumber, task, -1); // 使用负数优先级
    }

    // 取消特定任务
    public synchronized void cancelTask(String phoneNumber, int taskId) {
        ConcurrentHashMap<Integer, PhoneTask> taskIdMap = taskMap.get(phoneNumber);
        if (taskIdMap != null) {
            PhoneTask phoneTask = taskIdMap.remove(taskId);
            if (phoneTask != null) {
                PriorityBlockingQueue<PhoneTask> queue = queueMap.get(phoneNumber);
                if (queue != null) {
                    queue.remove(phoneTask);
                }
            }
        }
    }

    // 取消某个号码的所有任务
    public synchronized void cancelAllTasks(String phoneNumber) {
        ConcurrentHashMap<Integer, PhoneTask> taskIdMap = taskMap.remove(phoneNumber);
        if (taskIdMap != null) {
            PriorityBlockingQueue<PhoneTask> queue = queueMap.remove(phoneNumber);
            if (queue != null) {
                queue.clear();
            }
        }
    }

    // 开始处理任务
    public void startProcessing() {
        queueMap.forEach((phoneNumber, queue) -> {
            executorService.submit(() -> {
                while (true) {
                    try {
                        PhoneTask phoneTask = queue.take();
                        phoneTask.getTask().run();
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        break;
                    }
                }
            });
        });
    }

    private static class PhoneTask implements Comparable<PhoneTask> {
        private final Runnable task;
        private final int priority;
        private final int taskId;

        public PhoneTask(Runnable task, int priority, int taskId) {
            this.task = task;
            this.priority = priority;
            this.taskId = taskId;
        }

        public Runnable getTask() {
            return task;
        }

        @Override
        public int compareTo(PhoneTask other) {
            int result = Integer.compare(this.priority, other.priority);
            return result != 0 ? result : Integer.compare(this.taskId, other.taskId);
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) return true;
            if (obj == null || getClass() != obj.getClass()) return false;
            PhoneTask other = (PhoneTask) obj;
            return taskId == other.taskId;
        }

        @Override
        public int hashCode() {
            return Integer.hashCode(taskId);
        }
    }
}
